package com.example.chenjun.myapplication.net.retrofit;

import android.os.Handler;
import android.os.Looper;

import com.example.chenjun.myapplication.net.BaseLoader;
import com.example.chenjun.myapplication.net.NetController;
import com.example.chenjun.myapplication.net.netcallback.BaseCallback;
import com.example.chenjun.myapplication.net.retrofit.api.GetFormService;
import com.example.chenjun.myapplication.net.retrofit.api.PostFileService;
import com.example.chenjun.myapplication.net.retrofit.api.PostFormService;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RetrofitLoader extends BaseLoader {
    private static final Handler H = new Handler(Looper.getMainLooper());
    public RetrofitLoader(NetController controller) {
        super(controller);
    }

    @Override
    public String enqueue() {
        return null;
    }

    @Override
    public void execute(final BaseCallback callback) {
        Call<ResponseBody> call = null;
        switch (mController.getMethod()) {
            case POST:
                Map<String, File> files = mController.getFiles();
                if (files != null && !files.isEmpty()){
                    PostFileService postFileService = RetrofitUtil.getRetrofitInstance().create(PostFileService.class);
                    Set<String> fileKeySet = files.keySet();
                    Map<String, RequestBody> bodyMap = new HashMap<>();
                    for (String key : fileKeySet) {
                        RequestBody body = RequestBody.create(null, files.get(key));
                        bodyMap.put(key, body);
                    }
                    call = postFileService.postFile(mController.getHeaders(), mController.getParams(), bodyMap);

                }else {
                    PostFormService service = RetrofitUtil.getRetrofitInstance().create(PostFormService.class);
                    call = service.getData(mController.getHeaders(), mController.getUrl(), mController.getParams());
                }
                break;
            case GET:
                GetFormService getFormService = RetrofitUtil.getRetrofitInstance().create(GetFormService.class);
                call = getFormService.getData(mController.getHeaders(), mController.getUrl(), mController.getParams());
                break;
            default:
                break;
        }
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(final Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    ResponseBody body = response.body();
                    final String string = body.string();
                    H.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            callback.onResponse(callback.parse(string));
                        }catch (NullPointerException e){
                            callback.onResponse(string);
                        }
                    }
                });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                callback.onError(new Exception(t));
            }
        });
//        new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call call, Response response) {
//                H.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        callback.onResponse(callback.parse(str));
//                    }
//                });
//            }
//
//            @Override
//            public void onFailure(Call call, Throwable t) {
//                callback.onError(new Exception(t));
//            }
//        }
    }

    @Override
    public File downFile() {
        return null;
    }
}
